#!/bin/bash
set -e

# 配置参数
SERVER_IP="101.43.36.81"
FRONTEND_DIR="./frontend"
BACKEND_DIR="./backend"
JAR_NAME="qiyeshapan-0.0.1-SNAPSHOT.jar"
SERVICE_NAME="qiyeshapan.service"

# 打包前端
echo "Building frontend..."
cd $FRONTEND_DIR
npm install
npm run build
tar -czvf dist.tar.gz dist
scp dist.tar.gz $SERVER_IP:/opt/
ssh $SERVER_IP "rm -rf /opt/dist/* && tar -xzvf /opt/dist.tar.gz -C /opt/ && rm /opt/dist.tar.gz"
rm dist.tar.gz
cd ..
echo "Frontend deployed!"

# 打包后端
echo "Building backend..."
cd $BACKEND_DIR
mvn clean package -DskipTests
scp target/$JAR_NAME $SERVER_IP:/opt/backend/
cd ..
echo "Backend deployed!"

# 重启后端服务
echo "Restarting backend service..."
ssh $SERVER_IP "sudo systemctl restart $SERVICE_NAME"

echo "Deployment complete! Service status:"
ssh $SERVER_IP "sudo systemctl status $SERVICE_NAME --no-pager" 